/*
 * @ Created with Clion
 * @ Author Morbid
 * @ Date 2022/06/12
 * @ Time 11:22
 */

#include <cstdio>
#include <cstdlib>
#include "LinkList.h"

/*!
 * 尾插法建立不带无单链表
 * @param L 不带单链表
 * @return 不带头单链表
 */
LinkList List_TailInsert_NoneHead(LinkList &L) {
    int x;
    L = (LinkList) malloc(sizeof(LNode));
    scanf("%d", &x);
    if (x != 9999) {
        auto *i = (LNode *) malloc(sizeof(LNode));
        i->data = x;
        i->next = L;
        L = i;
    } else {
        L = nullptr;
        return L;
    }
    LNode *p, *q = L;
    scanf("%d", &x);
    while (x != 9999) {
        p = (LinkList) malloc(sizeof(LNode));
        p->data = x;
        q->next = p;
        q = p;
        scanf("%d", &x);
    }
    q->next = nullptr;
    return L;
}

/*!
 * 尾插法建立带头单链表
 * @param L 带头单链表
 * @return 带头单链表
 */
LinkList List_TailInsert_HaveHead(LinkList &L) {
    int x;
    L = (LinkList) malloc(sizeof(LNode));
    LNode *p, *q = L;
    scanf("%d", &x);
    while (x != 9999) {
        p = (LinkList) malloc(sizeof(LNode));
        p->data = x;
        q->next = p;
        q = p;
        scanf("%d", &x);
    }
    q->next = nullptr;
    return L;
}

/*!
 * 头插法建立不带头单链表
 * @param L 不带头单链表
 * @return 不带头单链表
 */
LinkList List_HeadInsert_NoneHead(LinkList &L) {
    LNode *p;
    int x;
    L = (LinkList) malloc(sizeof(LNode));
    scanf("%d", &x);
    auto *i = (LNode *) malloc(sizeof(LNode));
    if (x != 9999) {
        i->data = x;
        i->next = L;
        L = i;
    } else {
        L = nullptr;
        return L;
    }
    L->next = nullptr;
    scanf("%d", &x);
    while (x != 9999) {
        p = (LinkList) malloc(sizeof(LNode));
        p->data = x;
        p->next = L;
        L = p;
        scanf("%d", &x);
    }
    return L;
}

/*!
 * 头插法建立带头单链表
 * @param L 带头单链表
 * @return 带头单链表
 */
LinkList List_HeadInsert_HaveHead(LinkList &L) {
    LNode *p;
    int x;
    L = (LinkList) malloc(sizeof(LNode));
    L->next = nullptr;
    scanf("%d", &x);
    while (x != 9999) {
        p = (LinkList) malloc(sizeof(LNode));
        p->data = x;
        p->next = L->next;
        L->next = p;
        scanf("%d", &x);
    }
    return L;
}

int main() {
    LinkList L;
    InitList_NoneHead(L);
    for (int i = 1; i <= 5; i++)
        ListInsert_NoneHead(L, i, i);
    printf("\nlen:%d\n", Length_NoneHead(L));
    for (int i = 1; i <= Length_NoneHead(L); i++)
        printf("%d  ", i, GetElem_NoneHead(L, i)->data);
    if (Empty_NoneHead(L))
        printf("空表");
    return 0;
}